$(function(){

	//按钮一
    var type,year,month,date,textName,fuText;
    var searchb = function()
    {
    	type = $('#basicType').val();
        year = $('#basicYear').val();
        month = $('#basicMonth').val();
    	date = year + "-"+month;
    	$('.reportMonth').show();
    	if(type == 0)
	    {
			url = "/modules/operatingStatistics/tenderBaseInfo.html?date=" + date;
	    	ajaxUrl2(url,type);
	    }
	    else if(type == 1)
	    {
	    	$('.reportMonth').show();
	   		url = "/modules/operatingStatistics/tenderAreaDistributed.html?date=" + date;
	   		ajaxUrl2(url,type);
	    }
    }
    
	var ajaxUrl2 = function (url,type){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var option;
				if(type == 0)//type0 饼图
				{
					var sums = 0;
					for (var i = 0; i < data.arr.length; i++) 
					{
						sums = sums+data.arr[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
						if(data.arr[i] == 0)
						{
							delete data.arr[i];
						}
					};
					option1 = {
						title : {
						    text: '投资人数年龄分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['10-20岁','20-30岁','30-40岁','40-50岁','50岁以上']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						calculable : true,
						series : [
						    {
						        name:'年龄分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[0],name:'10-20岁'},
						            {value:data.arr[1],name:'20-30岁'},
						            {value:data.arr[2],name:'30-40岁'},
						            {value:data.arr[3],name:'40-50岁'},
						            {value:data.arr[4],name:'50岁以上'}
						        ]
						    }
						]
					}
					option2 = {
						title : {
						    text: '投资人数性别分布',
						    x : 'center',
					        y : 'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['男','女']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						series : [
						    {
						        name:'性别分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[5],name:'女'},
						            {value:data.arr[6],name:'男'}
						        ]
						    }
						]
					}
					option3 = {
						title : {
						    text: '投资人数文化程度分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['小学','初中','高中','中专','大专','本科','硕士','博士']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						series : [
						    {
						        name:'文化程度分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[7],name:'小学'},
						            {value:data.arr[8],name:'初中'},
						            {value:data.arr[9],name:'高中'},
						            {value:data.arr[10],name:'中专'},
						            {value:data.arr[11],name:'大专'},
						            {value:data.arr[12],name:'本科'},
						            {value:data.arr[13],name:'硕士'},
						            {value:data.arr[14],name:'博士'}
						        ]
						    }
						]
					}
					option4 = {
						title : {
						    text: '投资人数婚姻状况分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['未婚','已婚','离异']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						series : [
						    {
						        name:'婚姻状况分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[15],name:'未婚'},
						            {value:data.arr[16],name:'已婚'},
						            {value:data.arr[17],name:'离异'},
						            {value:data.arr[18],name:'丧偶'}
						        ]
						    }
						]
					}
					document.getElementById('investTableb').style.width = "48%";
					document.getElementById('investTablec').style.display = "inline-block";
					document.getElementById('investTabled').style.display = "inline-block";
					document.getElementById('investTablee').style.display = "inline-block";
					echarts.init(document.getElementById('investTableb')).setOption(option1); 
			        echarts.init(document.getElementById('investTablec')).setOption(option2); 
			        echarts.init(document.getElementById('investTabled')).setOption(option3); 
			        echarts.init(document.getElementById('investTablee')).setOption(option4); 
					                    
				}
				else if(type == 1)//type1地图
				{
					var dataVal = [];
					var sums = 0;
					if(data.counts.length == 0)
					{
						dataVal.push({name: '暂无数据',value:0});
						fuText = '暂无数据';
					}
					else
					{
						for (var i = 0; i < data.counts.length; i++) 
						{
							dataVal.push({name: data.provinces[i],value: data.counts[i]});
						};
						fuText = '';
					}
					
					option = {
					    title : {
					        text: '各省人数分布',
					        x:'center',
					        subtext:fuText
					    },
					    tooltip : {
					    	show:true,
					        trigger: 'axis'
					    },
					    legend: {
					        orient: 'vertical',
					        x:'left',
					        data:['人数（人）']
					    },
					    dataRange: {
					        min: 0,
					        max: 25000,
					        x: 'left',
					        y: 'bottom',
					        text:['高','低'],           // 文本，默认为数值文本
					        calculable : true
					    },
					    roamController: {
					        show: true,
					        x: 'right',
					        mapTypeControl: {
					            'china': true
					        }
					    },
					    series : [
					        {
					            name: '人数（人）',
					            type: 'map',
					            mapType: 'china',
					            roam:true,
					            itemStyle:{
					                normal:{label:{show:true}},
					                emphasis:{label:{show:true}}
					            },
					            data:dataVal
					        }
					    ]
					};
                    
					                      
			        document.getElementById('investTableb').style.width = "100%";
			        document.getElementById('investTablec').style.display = "none";
					document.getElementById('investTabled').style.display = "none";
					document.getElementById('investTablee').style.display = "none";
					echarts.init(document.getElementById('investTableb')).setOption(option); 
				}
			}
		});
	};

	var changBtn1 = $('.investorsTabs .tabs-header ul li a:eq(0)');
	changBtn1.click(function(){
		searchb();//初始化加载
	});
	searchb();
    $("#basicType,#basicYear,#basicMonth,#borrowType").change(function(){
    	searchb();
    });





    //报表类型 按钮二

    var search = function ()
    {
    	type = $('#type').val();
        year = $('#year').val();
    	month = $('#month').val();
    	date = year + "-"+month;
    	textName = $("#type[name=type]").find("option:selected").text();
    	if(type == 0)
	    {
	   		$('.reportMonth').hide();
	    	url = "/modules/operatingStatistics/tenderAnalysis.html?type=" + type + "&year=" + year;
	    	ajaxUrl(url,type,textName);
	    }
	    else if(type == 1 || type == 2)
	    {
	    	$('.reportMonth').show();
	   		url = "/modules/operatingStatistics/tenderDistributed.html?type=" + type + "&date=" + date;
	   		ajaxUrl(url,type,textName);
	    	
	    }
    }

	var ajaxUrl = function (url,type,textName){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				if(data.distributed)
				{
					var sums = 0;
					for (var i = 0; i < data.distributed.length; i++) {
						sums = sums+data.distributed[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
						if(data.distributed[i] == 0)
						{
							delete data.distributed[i];
						}
					};
				}
				var option;
				if(type == 0)//柱状图
				{
					var sums = 0;
					for(var i=0;i<data.counts.length;i++)
					{
						sums = sums+data.counts[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
					}
					option = {
			        	color  : ['#70bc31'],
			            tooltip: {
			                show: true
			            },
			            title: {
			            	text: textName,
			            	x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
			            },
			            grid: {
			            	x:80,
			            	y:80,
			            	x2:30,
			            	y2:70
			           	},
			            xAxis : [
			                {
			                   type : 'category',
			                   data : data.dates
			                }
			            ],
			            yAxis : [
			                {
			                	type:'value',
			                    name:'人数（人）',
			                }
			            ],
			            series : [
			                {
			                    "name":"人数（人）",
			                    "type":"bar",
			                    "data":data.counts
			                }
			            ]
			        };
				}
				else if(type == 1)//饼图
				{
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						legend: {
					        orient : 'vertical',
					        x : '70%',
					        y : 80,
					        data:['0-1万元','1-10万元','10-50万元','50-100万元','100万元以上']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						calculable : true,
						series : [
						    {
						        name:'金额分布',
						        type:'pie',
						        radius : '55%',
						        center: ['50%', '60%'],
						        data:[
						            {value:data.distributed[0],name:'0-1万元'},
						            {value:data.distributed[1],name:'1-10万元'},
						            {value:data.distributed[2],name:'10-50万元'},
						            {value:data.distributed[3],name:'50-100万元'},
						            {value:data.distributed[4],name:'100万元以上'},
						        ]
						    }
						]
					}
				}
				else if(type == 2)//饼图
				{
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						legend: {
					        orient : 'vertical',
					        x : '70%',
					        y : 80,
					        data:['0-1万元','1-10万元','10-50万元','50-100万元','100万元以上']
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						calculable : true,
						series : [
						    {
						        name:'人数分布',
						        type:'pie',
						        radius : '55%',
						        center: ['50%', '60%'],
						        data:[
						            {value:data.distributed[0],name:'0-1万元'},
						            {value:data.distributed[1],name:'1-10万元'},
						            {value:data.distributed[2],name:'10-50万元'},
						            {value:data.distributed[3],name:'50-100万元'},
						            {value:data.distributed[4],name:'100万元以上'},
						        ]
						    }
						]
					}
				}
				// 基于准备好的dom，初始化echarts图表
		        var myChart = echarts.init(document.getElementById('investTablea')); 
		        // 为echarts对象加载数据 
		        myChart.setOption(option); 
			}
		});
	};
	var changBtn2 = $('.investorsTabs .tabs-header ul li a:eq(1)');
	changBtn2.click(function(){
		search();//初始化加载
	});
    $("#type,#year,#month").change(function(){
		search();
    });

});
